package cn.mxz.xmu.robots.library.xmu;

import java.util.HashMap;
import java.util.Map;

import wang.mouge.dubbo.service.crawler.Request;
import cn.mxz.xmu.robots.library.basic.Library;
import cn.mxz.xmu.robots.library.utils.HuiwenUtils;

/**
 * @school 厦门大学图书馆(1)
 * @student 30920112202553 leezqyuan | 15220132201585 143725
 * @Url http://210.34.4.28
 * @function 　读者信息，当前借阅，历史借阅，
 * 
 * @author dongchao <dongchao@mouge.wang>
 * @since 2015-07-23
 */
public class XmuLibrary extends Library
{

	public static void main(String[] args)
	{
		XmuLibrary library = new XmuLibrary();
		System.out.println(library.queryBooks("狼"));
		// System.out.println(library.bookDetails("http://210.34.4.28/opac/item.php?marc_no=0000300748"));
	}

	public XmuLibrary()
	{
		super();
	}

	@Override
	public String queryBooks(String params)
	{
		Request queryBooksRequest = new Request(buildUrl("queryBooks", params));
		queryBooksRequest.addHeader("Referer", "http://210.34.4.28/opac/openlink.php");
		queryBooksRequest.addParams("historyCount", "1");
		queryBooksRequest.addParams("strText", params);
		queryBooksRequest.addParams("doctype", "ALL");
		queryBooksRequest.addParams("strSearchType", "title");
		queryBooksRequest.addParams("match_flag", "forward");
		queryBooksRequest.addParams("displaypg", "200");
		queryBooksRequest.addParams("sort", "CATA_DATE");
		queryBooksRequest.addParams("orderby", "desc");
		queryBooksRequest.addParams("showmode", "list");
		queryBooksRequest.addParams("location", "ALL");

		String queryBookHtml = curlPost(queryBooksRequest);
		return HuiwenUtils.queryBooksParse(queryBookHtml, "#content .book_list_info", "http://210.34.4.28/opac/");

	}

	@Override
	public String bookDetails(String url)
	{
		Request descBookRequest = new Request(url);

		String descHtml = curlPost(descBookRequest);

		/* 馆藏地JSONArrary */
		Map<String, String> locationMap = new HashMap<String, String>();
		locationMap.put("index_num", "0");
		locationMap.put("bar_code", "1");
		locationMap.put("address", "3");
		locationMap.put("status", "4");
		String locationJson = HuiwenUtils.localsParse(descHtml, "table", locationMap);

		return HuiwenUtils.descBookParse(descHtml, "#item_detail .booklist", locationJson, "", "");
	}

	@Override
	protected String buildUrl(String type, String params)
	{
		StringBuilder url = new StringBuilder("http://210.34.4.28");
		switch (type)
		{
		/* 图书检索 */
		case "queryBooks":
			url.append("/opac/openlink.php");
			break;
		}
		return url.toString();
	}

}